﻿using BaseData.Domain;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Approval;
using BaseData.Infrastructure.DTO;
using BaseData.Infrastructure.Metting;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.ReadAPI.Controllers
{
    /// <summary>
    /// 会议管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [ApiExplorerSettings(GroupName = "Metting")]
    public class MettingController : ControllerBase
    {
        private readonly IMettingService metSer;
        private readonly IApprovalService appSer;

        public MettingController(IMettingService metSer,IApprovalService appSer)
        {
            this.metSer = metSer;
            this.appSer = appSer;
        }

        #region 会议室管理
        /// <summary>
        /// 添加会议室
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddMetting(MettingRoom room)
        {
            var res = await metSer.AddMetting(room);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "添加会议室成功",
                Data = res
            };
        }
        /// <summary>
        /// 显示会议室位置信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<MettingRoomAddress>>> GetRoomAddress()
        {
            var res = await metSer.GetAddress();
            return new APIResult<List<MettingRoomAddress>>
            {
                Code = CodeEnum.Success,
                Message = "显示会议室位置",
                Data = res
            };
        }
        /// <summary>
        /// 显示会议
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <param name="name">会议室名称</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<PaggingResult<List<MettingDto>>>> GetMetting(int index, int size, string? name)
        {
            var res = await metSer.GetMetting(index, size, name);
            return new APIResult<PaggingResult<List<MettingDto>>>
            {
                Code = CodeEnum.Success,
                Message = "显示会议管理成功",
                Data = res
            };
        }
        /// <summary>
        /// 软删除会议
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIResult<int>> DelMetting(int id)
        {
            var res = await metSer.DelMetting(id);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "删除会议成功",
                Data = res
            };
        }
        /// <summary>
        /// 修改会议
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdMetting(MettingRoom dto)
        {
            var res = await metSer.UpdMetting(dto);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "修改会议成功",
                Data = res
            };
        }
        #endregion

        #region 会议室预约管理
        /// <summary>
        /// 添加会议室预约
        /// </summary>
        /// <param name="book">实体</param>
        /// <returns>返回添加数据</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddMettingBook(MettingBooking book)
        {
            var res = await metSer.AddMettingBook(book);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "添加预约成功",
                Data = res
            };
        }
        /// <summary>
        /// 显示会议模板
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<MeetingTemplate>>> GetMeetingTemplates()
        {
            var res = await metSer.GetMeetingTemplates();
            return new APIResult<List<MeetingTemplate>>
            {
                Code = CodeEnum.Success,
                Message = "显示会议模板成功",
                Data = res
            };
        }
        /// <summary>
        /// 显示会议预约
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="templateId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<PaggingResult<List<MettingBookDto>>>> GetMettingBook(int index, int size, int templateId)
        {
            var res = await metSer.GetMettingBook(index, size, templateId);
            return new APIResult<PaggingResult<List<MettingBookDto>>>
            {
                Code = CodeEnum.Success,
                Message = "显示会议成功",
                Data = res
            };
        }
        /// <summary>
        /// 删除会议预约
        /// </summary>
        /// <param name="BookId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIResult<int>> DelMettingBook(int BookId)
        {
            var res = await metSer.DelMettingBook(BookId);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "删除会议成功",
                Data = res
            };
        }
        /// <summary>
        /// 修改会议预约
        /// </summary>
        /// <param name="book"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdMettingBook(MettingBooking book)
        {
            var res = await metSer.UpdMettingBook(book);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "修改会议成功",
                Data = res
            };
        }
        #endregion

        #region 审批管理
        /// <summary>
        /// 显示审批
        /// </summary>
        /// <param name="state"></param>
        /// <param name="subject"></param>
        /// <param name="mettingroom"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<ApprovalDTO>>> ShowApprove(int state, string? subject, string? mettingroom)
        {
            var res = await appSer.ShowApprove(state, subject, mettingroom);
            return new APIResult<List<ApprovalDTO>>
            {
                Code = CodeEnum.Success,
                Message = "显示审批成功",
                Data = res
            };
        }
        /// <summary>
        /// 审批会议
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> ApprovalMetting(int id, int state, string? remark)
        {
            var res = await appSer.ApprovalMetting(id, state, remark);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "审批成功",
                Data = 1
            };
        }
        #endregion
    }
}
